import { assembyEnvVar } from "./src/utils/vite";
import { ConfigEnv, defineConfig, UserConfig, loadEnv } from "vite";
import react from "@vitejs/plugin-react";
import { resolve } from "path";
import { createSvgIconsPlugin } from "vite-plugin-svg-icons";

// https://vitejs.dev/config/
export default defineConfig((mode: ConfigEnv): UserConfig => {
  const envVar = assembyEnvVar(loadEnv(mode.mode, process.cwd()));
  return {
    plugins: [
      react(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [resolve(process.cwd(), "src/assets/svg")],
        // 指定symbolId格式
        symbolId: "icon-[dir]-[name]",
      }),
    ],

    // 路径别名配置
    resolve: {
      alias: {
        "@": resolve(__dirname, "./src"),
      },
      extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json"],
    },

    // 服务器
    server: {
      host: true,
      port: envVar.VITE_PORT,
      open: envVar.VITE_OPEN_BROWSER,
      proxy: {
        "/api": {
          target: "http://127.0.0.1:8000",
          changeOrigin: true,
          rewrite: (path) => {
            return path.replace(/\/api/, "");
          },
        },
      },
    },

    // build
    build: {
      sourcemap: true,
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true,
        },
      },
    },
  };
});
